package com.mz.ozy.activity.shop;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mz.ozy.R;
import com.mz.ozy.adapter.CommentTagAdapter;
import com.mz.ozy.adapter.CourseCommentAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.CourseCommentBean;
import com.mz.ozy.mvp.presenter.TrainCommentPresenter;
import com.mz.ozy.mvp.view.TrainCommentContract;
import com.mz.ozy.widget.flowlayout.FlowLayout;
import com.mz.ozy.widget.flowlayout.TagFlowLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CommentActivity extends BaseActivity<TrainCommentContract.View, TrainCommentPresenter> implements TrainCommentContract.View {

    public static final String TYPE_CID = "TYPE_CID";

    @BindView(R.id.rv_course_comment)
    RecyclerView mRvCourseComment;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.flowTagLayout1)
    TagFlowLayout mFlowTagLayout1;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private CommentTagAdapter mSearchTagAdapter;
    private CourseCommentAdapter courseCommentAdapter;
    private int tag;
    private int page;
    private int type;
    private String mCid;
    private List<String> mList = new ArrayList<>();
    private boolean isFirstLoad = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("评价");

        initData();
        initView();
        initRecyclerView();
        initRefreshLayout();
        mRefreshLayout.autoRefresh();
    }

    private void getTestData() {
        mList.add("全部");
        mList.add("有图");
    }

    private void initView() {

    }

    private void initData() {
        Intent intent = getIntent();
        mCid = intent.getStringExtra(TYPE_CID);
    }

    private void initRefreshLayout() {
        mRefreshLayout.setEnableLoadmore(true);
        mRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                tag = 1;
                page = page + 1;
                mPresenter.queryCommentList(CommentActivity.this, mCid, type + "", page);

            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshCommentList();
            }
        });
    }

    private void refreshCommentList() {
        tag = 0;
        page = 1;
        mPresenter.queryCommentList(CommentActivity.this, mCid, type + "", page);
    }

    private void initRecyclerView() {
        mRvCourseComment.setLayoutManager(new LinearLayoutManager(this));
//        courseCommentAdapter = new CourseCommentAdapter(getTestCourseList());
        courseCommentAdapter = new CourseCommentAdapter(this, new ArrayList<>());
        mRvCourseComment.setAdapter(courseCommentAdapter);
    }

    @Override
    public void getCommentList(JSONObject result) {
        JSONObject page = result.optJSONObject("p");
        if (isFirstLoad) {
            initSelectView(result);
        }
        isFirstLoad = false;
        List<CourseCommentBean> lists = new Gson().fromJson(page.optString("pageItems"), new TypeToken<ArrayList<CourseCommentBean>>() {
        }.getType());
        if (tag == 0) {  //刷新
            mRefreshLayout.finishRefresh();
            if (lists != null && lists.size() > 0) {
                llNoData.setVisibility(View.GONE);
                mRvCourseComment.setVisibility(View.VISIBLE);
                courseCommentAdapter.setNewData(lists);
            } else {
                llNoData.setVisibility(View.VISIBLE);
                mRvCourseComment.setVisibility(View.GONE);
            }
        } else {  //加载更多
            mRefreshLayout.finishLoadmore();
            if (lists != null && lists.size() > 0) {
                courseCommentAdapter.addData(lists);
            }
        }
//判断是否已到达最后一页，为true，将不会再次触发加载更多事件
        mRefreshLayout.setLoadmoreFinished(page.optInt("pageSize") * page.optInt("pageNumber") >= page.optInt("pageTotle"));

    }

    private void initSelectView(JSONObject result) {

        final String tNum = result.optString("tNum");
        final String allNum = result.optString("allNum");
        mList.clear();
        mList.add("全部(" + allNum + ")");
        mList.add("有图(" + tNum + ")");

        mSearchTagAdapter = new CommentTagAdapter(this, mFlowTagLayout1, mList);
        mFlowTagLayout1.setAdapter(mSearchTagAdapter);
        mFlowTagLayout1.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                mSearchTagAdapter.setSelectPosition(position);
                type = position;
                refreshCommentList();
                return false;
            }
        });
    }
}
